package com.system.common.controller.person;

import com.system.common.constant.BaseEnum;
import com.system.common.constant.SessionKeyConst;
import com.system.common.request.BaseReq;
import com.system.common.request.person.AssignRoleReq;
import com.system.common.request.person.SavePersonReq;
import com.system.common.response.BaseResp;
import com.system.common.response.PageResp;
import com.system.common.service.persion.PersonService;
import com.system.common.vo.person.PersonInfoVo;
import com.system.common.vo.person.PersonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin/person")
public class PersonController {

    @Autowired
    private PersonService personService;


    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public String list() {
        return "/admin/person/list";
    }


    @RequestMapping(value = "/search", method = {RequestMethod.GET})
    @ResponseBody
    public PageResp search(SavePersonReq req) {
        return personService.search(req);
    }

    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ResponseBody
    public BaseResp save(SavePersonReq req) {

        return personService.savePerson(req);
    }

    @RequestMapping(value = "/del", method = {RequestMethod.POST})
    @ResponseBody
    public BaseResp del(BaseReq req) {
        return personService.delPerson(req);
    }

    /**
     *  给人员分配角色
     * @return
     */

    @RequestMapping(value = "/assignRole", method = {RequestMethod.POST})
    @ResponseBody
    public BaseResp assignRole(@RequestBody AssignRoleReq req){

        return personService.assignRole(req);
    }


    @RequestMapping(value = "/info", method = {RequestMethod.GET})
    @ResponseBody
    public BaseResp info(HttpServletRequest req) {
        BaseResp resp = new BaseResp();
        PersonInfoVo data = new PersonInfoVo();
        HttpSession session = req.getSession();
        Object obj = session.getAttribute(SessionKeyConst.PERSON_INFO);
        if(obj!=null){
            PersonVo personInfo = (PersonVo)obj;
            data.setName(personInfo.getLoginName());
        }
        resp.setCode(BaseEnum.SUCCESS.getCode());
        resp.setData(data);
        return resp;
    }


}
